package cn.edu.imust.loginregisterdemo.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;

public class RectangleToCircleUtils {

    /**
     * Crops a circle out of the thumbnail photo.
     */
    public static Bitmap getCroppedBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        //设置一个图片大小的矩形
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        //bm是一个刚好canvas大小的空Bitmap ，画完后应该会自动保存到bm
        Canvas canvas = new Canvas(output);


        final Paint paint = new Paint();
        paint.setAntiAlias(true);


        int halfWidth = bitmap.getWidth()/2;
        int halfHeight = bitmap.getHeight()/2;
        //画圆
        canvas.drawCircle(halfWidth, halfHeight, Math.max(halfWidth, halfHeight), paint);
        //设置为取两层图像交集部门,只显示上层图像
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //画图像
        canvas.drawBitmap(bitmap, rect, rect, paint);


        return output;
    }
}
